package com.foodost.idiomer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class LifeImageView extends ImageView {
    
    private Drawable lifeImage;
    private int num,lifes;
    private int lifeWidth,lifeHeight;
    public LifeImageView(Context context) {
        super(context);
        
    }

    public LifeImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    
    public LifeImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LifeImageView); 
        lifeImage = array.getDrawable(R.styleable.LifeImageView_src);
        num = array.getInt(R.styleable.LifeImageView_num, 3);
        lifes = num;
        if(lifeImage != null){
            setLifeImage(lifeImage);
        }
    }
    
    public void setLifeImage(Drawable lifeImage){
        this.lifeImage = lifeImage;
        lifeWidth = lifeImage.getIntrinsicWidth();
        lifeHeight = lifeImage.getIntrinsicHeight();
    }
    
    public void setLifes(int lifes){
        this.lifes = lifes;
    }
    
    public int getLifes(){
        return lifes;
    }
    
    public int getAllLifes(){
        return num;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setFilterBitmap(true);
        int width = getWidth();
        int height = getHeight();
        double boundw= (width*0.8 - lifeWidth)/num;
        double boundh = (height*0.8 - lifeHeight)/num;
        if(lifes == 0){
            drawabletoGray(lifeImage);
            Bitmap bitmap = drawableToBitmap(lifeImage);
            canvas.drawBitmap(bitmap, (float)(width*0.15 + boundw)/2, (float)(height*0.15 + boundh)/2, p);
            return;
        }
        Bitmap bitmap = drawableToBitmap(lifeImage);
        for(int i =0; i < lifes; i ++){
            canvas.drawBitmap(bitmap, (float)(width*0.15 + i*boundw), (float)(height*0.15 + i*boundh), p);
        }
    }
    
    public  Bitmap drawableToBitmap(Drawable drawable) { 
        
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), 
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); 
        Canvas canvas = new Canvas(bitmap); 
     
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
        drawable.draw(canvas); 
        return bitmap; 
    } 
    
    
    //drawable to gray
    public void drawabletoGray(Drawable mDrawable){
        mDrawable.mutate();   
        ColorMatrix cm = new ColorMatrix();   
        cm.setSaturation(0);   
        ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);   
        mDrawable.setColorFilter(cf);  
    }
    
    
    

}
